import 'package:flustars/flustars.dart';
import 'package:get/get_state_manager/get_state_manager.dart';

class SpUtils {
  /// 初始化SharedPreferences
  static void initInfo() async {
    SpUtil.getInstance();
  }

  /// 保存string
  static bool putString(String key, String value) {
    bool isPut = SpUtil.putString(key, value) as bool;
    return isPut;
  }

  /// 根据key获取值，默认’‘
  static String getString(String key) {
    return SpUtil.getString(key, defValue: '');
  }

  /// 保存对象
  static bool putObject(String key, Object value) {
    bool isPut = SpUtil.putObject(key, value) as bool;
    return isPut;
  }

  /// 根据key取对象
  static Object getObject(String key) {
    return SpUtil.getObject(key);
  }

  /// 根据key移除
  static Object remove(String key) {
    return SpUtil.remove(key);
  }

  /// 清除
  static bool clear() {
    var clear = SpUtil.clear() as bool;
    return clear;
  }

  /// 判断key是否存在
  static bool haveKey(String key) {
    return SpUtil.haveKey(key);
  }
}

/**
 * 
 
 /// 等待Sp初始化完成。
await SpUtil.getInstance();  
  
/// 同步使用Sp。支付默认值。
String name = SpUtil.putString("key_username", "Sky24n");
bool isShow = SpUtil.getBool("key_show", defValue: true);
  
/// 存储实体对象示例。
City city = new City();
city.name = "成都市";
SpUtil.putObject("loc_city", city);
    
City hisCity = SpUtil.getObj("loc_city", (v) => City.fromJson(v));
print("City: " + (hisCity == null ? "null" : hisCit.toString()));
  
/// 存储实体对象list示例。
List<City> list = new List();
list.add(new City(name: "成都市"));
list.add(new City(name: "北京市"));
SpUtil.putObjectList("loc_city_list", list);
    
List<City> _cityList = SpUtil.getObjList("loc_city_list", (v) => City.fromJson(v));
print("City list: " + (_cityList == null ? "null" : _cityList.toString()));

 */
